package com.link.base.tencent.corpwx.usertag.util;

import com.alibaba.fastjson.JSONObject;
import com.link.base.wechat.basic.util.WxExceptionUtil;
import com.link.core.basic.service.BasicServiceException;
import com.link.core.util.HttpRequestUtil;
import com.link.core.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业微信 - 成员标签管理工具类
 *
 * @author 欧阳振强
 * @date 2020/01/06
 */
public class UserTagUtil {
    /**增加标签成员时，成员ID数量限制*/
    private static final int ADD_TAG_USER_MAX_USER_NUM = 1000;
    /**增加标签成员时，部门ID数量限制*/
    private static final int ADD_TAG_USER_MAX_PARTY_NUM = 100;
    /**编辑类型类型 - 添加*/
    private static final String EDIT_TAG_USER_FOR_ADD = "add";
    /**编辑类型类型 - 删除*/
    private static final String EDIT_TAG_USER_FOR_DEL = "del";
    /**创建标签接口*/
    private static final String CREATE_TAG_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token=ACCESS_TOKEN";
    /**更新标签接口*/
    private static final String UPDATE_TAG_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/update?access_token=ACCESS_TOKEN";
    /**删除标签接口*/
    private static final String DELETE_TAG_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/delete?access_token=ACCESS_TOKEN&tagid=TAGID";
    /**获取标签列表接口*/
    private static final String LIST_TAG_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/list?access_token=ACCESS_TOKEN";
    /**获取标签成员接口*/
    private static final String GET_TAG_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/get?access_token=ACCESS_TOKEN&tagid=TAGID";
    /**增加标签成员接口*/
    private static final String ADD_TAG_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/addtagusers?access_token=ACCESS_TOKEN";
    /**删除标签成员接口*/
    private static final String DEL_TAG_USER_API = "https://qyapi.weixin.qq.com/cgi-bin/tag/deltagusers?access_token=ACCESS_TOKEN";

    /**
     * 创建标签 - 默认标签ID直接使用企业微信的机制
     *
     * @author 欧阳振强
     * @date 2020/01/04
     * @param accessToken 接口凭证
     * @param tagName 标签名称
     */
    public static JSONObject createTag(String accessToken, String tagName) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(tagName)) {
            throw new BasicServiceException("标签名称不能为空！");
        }
        String url = CREATE_TAG_API.replace("ACCESS_TOKEN", accessToken);
        JSONObject obj = new JSONObject();
        obj.put("tagname", tagName);
        String param = obj.toJSONString();
        String resultStr = HttpRequestUtil.jsonPost(url, param);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 更新标签
     *
     * @author 欧阳振强
     * @date 2020/01/04
     * @param accessToken 接口凭证
     * @param tagId 标签Id
     * @param tagName 标签名称
     */
    public static JSONObject updateTag(String accessToken, String tagId, String tagName) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(tagId)) {
            throw new BasicServiceException("标签Id不能为空！");
        }
        if (StringUtils.isBlank(tagName)) {
            throw new BasicServiceException("标签名称不能为空！");
        }
        String url = UPDATE_TAG_API.replace("ACCESS_TOKEN", accessToken);
        JSONObject obj = new JSONObject();
        obj.put("tagid", tagId);
        obj.put("tagname", tagName);
        String param = obj.toJSONString();
        String resultStr = HttpRequestUtil.jsonPost(url, param);
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 删除标签
     *
     * @author 欧阳振强
     * @date 2020/01/04
     * @param accessToken 接口凭证
     * @param tagId 标签Id
     */
    public static JSONObject deleteTag(String accessToken, String tagId) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(tagId)) {
            throw new BasicServiceException("标签Id不能为空！");
        }
        String url = DELETE_TAG_API.replace("ACCESS_TOKEN", accessToken).replace("TAGID", tagId);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 获取标签列表
     *
     * @author 欧阳振强
     * @date 2020/01/04
     * @param accessToken 接口凭证
     */
    public static JSONObject listTag(String accessToken) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        String url = LIST_TAG_API.replace("ACCESS_TOKEN", accessToken);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 获取标签成员
     *
     * @author 欧阳振强
     * @date 2020/01/04
     * @param accessToken 接口凭证
     * @param tagId 标签Id
     */
    public static JSONObject getTagUser(String accessToken, String tagId) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(tagId)) {
            throw new BasicServiceException("标签Id不能为空！");
        }
        String url = GET_TAG_USER_API.replace("ACCESS_TOKEN", accessToken).replace("TAGID", tagId);
        String resultStr = HttpRequestUtil.sendGet(url, "");
        return WxExceptionUtil.parse(resultStr);
    }

    /**
     * 编辑标签成员（包括新增和删除）
     *
     * @author 欧阳振强
     * @date 2020/01/04
     * @param accessToken 接口凭证
     * @param type 编辑类型，添加add/删除del
     * @param tagId 标签Id
     * @param userIdList 成员Id列表
     * @param partyIdList 部门Id列表
     */
    public static JSONObject editTagUser(String accessToken, String type, String tagId, List<String> userIdList,
                                         List<String> partyIdList) throws Exception {
        if (StringUtils.isBlank(accessToken)) {
            throw new BasicServiceException("accessToken不能为空！");
        }
        if (StringUtils.isBlank(type)) {
            throw new BasicServiceException("编辑类型不能为空！");
        }
        if (StringUtils.isBlank(tagId)) {
            throw new BasicServiceException("标签Id不能为空！");
        }
        userIdList = userIdList == null ? new ArrayList<>() : userIdList;
        partyIdList = partyIdList == null ? new ArrayList<>() : partyIdList;
        if (userIdList.size() <= 0 && partyIdList.size() <= 0) {
            throw new BasicServiceException("成员Id列表和部门Id列表不能同时为空！");
        }
        if (userIdList.size() > ADD_TAG_USER_MAX_USER_NUM) {
            throw new BasicServiceException("成员Id列表长度不能大于" + ADD_TAG_USER_MAX_USER_NUM + "！");
        }
        if (partyIdList.size() > ADD_TAG_USER_MAX_PARTY_NUM) {
            throw new BasicServiceException("部门Id列表长度不能大于" + ADD_TAG_USER_MAX_PARTY_NUM + "！");
        }
        String url;
        if (EDIT_TAG_USER_FOR_ADD.equals(type)) {
            url = ADD_TAG_USER_API.replace("ACCESS_TOKEN", accessToken);
        } else if (EDIT_TAG_USER_FOR_DEL.equals(type)) {
            url = DEL_TAG_USER_API.replace("ACCESS_TOKEN", accessToken);
        } else {
            throw new BasicServiceException("非法编辑类型！");
        }
        JSONObject obj = new JSONObject();
        obj.put("tagid", tagId);
        obj.put("userlist", userIdList);
        obj.put("partylist", partyIdList);
        String param = obj.toJSONString();
        String resultStr = HttpRequestUtil.jsonPost(url, param);
        return WxExceptionUtil.parse(resultStr);
    }
}
